package com.example.webviewdemo08;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private WebView mWebView;
    private Button mBefore;
    private Button mNext;
    private long mStartTime;
    private Button mAndroidCallJs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mWebView = (WebView) findViewById(R.id.webview);

        mBefore = (Button) findViewById(R.id.before);
        mNext = (Button) findViewById(R.id.next);
        mBefore.setOnClickListener(this);
        mNext.setOnClickListener(this);

        mAndroidCallJs = (Button) findViewById(R.id.androidCallJs);
        mAndroidCallJs.setOnClickListener(this);

        WebSettings settings = mWebView.getSettings();

        settings.setJavaScriptEnabled(true);//支持JS
        settings.setBuiltInZoomControls(true);//显示缩放按钮
        settings.setUseWideViewPort(true);
        // mWebView.loadUrl("http://baidu.com/");


        mWebView.setWebViewClient(new WebViewClient() {
            //网页加载前
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
            }

            //网页加载后
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
            }
        });
        mWebView.setWebChromeClient(new WebChromeClient() {
            //在加载网页时,实时将加载进度条返回
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                System.out.println("newProgress:" + newProgress);
            }

            //获取对应网页标题
            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                System.out.println("标题" + title);
            }
        });
        mWebView.loadUrl("file:///android_asset/demo.html");//加载本地网页
        //第一个参数接口对象,
        // 第二个参数接口对象的名称,必须与js方法中的对象名完全一致
        mWebView.addJavascriptInterface(new JsCallAndroid() {
            //注意:接口方法必须与js方法中的方法名完全一致
            @JavascriptInterface//注意:此处一定要加该注释,否则在4.1+系统上运行失败
            @Override
            public void onCallback() {
                Toast.makeText(MainActivity.this, "js代码调用了Android代码", Toast.LENGTH_SHORT).show();
            }
        }, "demo");
    }

    public interface JsCallAndroid {
        public void onCallback();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.before:
                if (mWebView.canGoBack()) {//上一页
                    mWebView.goBack();
                } else {
                    Toast.makeText(this, "已经是第一页了", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.next:
                if (mWebView.canGoForward()) {//下一页
                    mWebView.goForward();
                } else {
                    Toast.makeText(this, "已经是最后一页了", Toast.LENGTH_SHORT).show();
                }

                break;
            case R.id.androidCallJs:
                //直接使用webview加载js就可以了
                //此行代码可以保证js的alert弹窗正常弹出
                //mWebView.setWebChromeClient(new WebChromeClient());
                mWebView.loadUrl("javascript:wave()");
                break;
        }
    }

    @Override
    public void onBackPressed() {//点击返回键的时候
        if (mWebView.canGoBack()) {
            mWebView.goBack();
        } else {
            long currentTime = System.currentTimeMillis();

            if ((currentTime - mStartTime) < 2000) {
                this.finish();
            } else {
                mStartTime = System.currentTimeMillis();
                Toast.makeText(this, "再点击一次返回,则推出应用", Toast.LENGTH_SHORT).show();

            }

        }
    }
}